#include "tabcontent.h"
#include "excel.h"

#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QComboBox>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QGroupBox>
#include <QSpacerItem>
#include <QHeaderView>
#include <QTableView>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QDebug>

TabContent::TabContent(QWidget *parent) : QWidget(parent)
{
    this->initWidget();
    this->initParameters();
}

QTableView *TabContent::getTableView()
{
    return this->tableView;
}

void TabContent::initWidget()
{
    QVBoxLayout *vLayout = new QVBoxLayout;

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(new QLabel("<b>筛选项目：</b>"));
    comboFilter = new QComboBox;
    comboFilter->addItem("默认");
    comboFilter->addItem("商品");
    comboFilter->addItem("日期");
    hLayout->addWidget(comboFilter);
    lineEditFilter = new QLineEdit;
    hLayout->addWidget(lineEditFilter);
    hLayout->addWidget(new QLabel("<b>排序项目（降序）：</b>"));
    comboOrder = new QComboBox;
    comboOrder->addItem("默认");
    comboOrder->addItem("日期");
    comboOrder->addItem("营收");
//    comboOrder->addItem("销量");
    hLayout->addWidget(comboOrder);
    pushBtnRefresh = new QPushButton("刷新");
    hLayout->addWidget(pushBtnRefresh);

    vLayout->addLayout(hLayout);
    tableView = new QTableView;
    vLayout->addWidget(tableView);
    QGroupBox *groupBox = new QGroupBox;
    QHBoxLayout *h = new QHBoxLayout(groupBox);
    h->addWidget(new QLabel("条数："));
    labelItemsCount = new QLabel("0");
    h->addWidget(labelItemsCount);
    h->addItem(new QSpacerItem(64, 26, QSizePolicy::Expanding, QSizePolicy::Minimum));
    pushBtnExport = new QPushButton("导出到Excel", groupBox);
    pushBtnExport->setMinimumWidth(200);
    h->addWidget(pushBtnExport);
    vLayout->addWidget(groupBox);

    this->setLayout(vLayout);
}

void TabContent::initParameters()
{
    // 奇数偶数行颜色交替
    tableView->setAlternatingRowColors(true);
    // 垂直表头是否可见
    tableView->verticalHeader()->setVisible(false);
    // 选中一行表头是否加粗
    tableView->horizontalHeader()->setHighlightSections(true);
    // 最后一列拉伸填充
    tableView->horizontalHeader()->setStretchLastSection(true);
    // 行标题最小宽度尺寸
    tableView->horizontalHeader()->setMinimumSectionSize(0);
    // 行标题最大高度
    tableView->horizontalHeader()->setMaximumHeight(100);
    // 默认行高
    tableView->verticalHeader()->setDefaultSectionSize(20);
    // 如何触发编辑
    tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    // 选中时一行整体选中
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    // 只允许选择单个
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);

    connect(pushBtnExport, &QPushButton::clicked, this, &TabContent::export2excel);
    connect(comboOrder, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TabContent::on_orderChanged);
    connect(comboFilter, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TabContent::on_filterChanged);
    connect(pushBtnRefresh, &QPushButton::clicked, [this](){
        QSqlTableModel *model = qobject_cast<QSqlTableModel*>(tableView->model());
        model->select();
        labelItemsCount->setText(QString::number(model->rowCount()));
    });
}


void TabContent::on_orderChanged(int index)
{
    QSqlTableModel *model = qobject_cast<QSqlTableModel*>(tableView->model());
    switch (index) {
    case 0:  // 默认，无排序,按id递增
        model->sort(0, Qt::AscendingOrder);
        break;
    case 1:  // 日期
        model->sort(model->fieldIndex("CreateTime"), Qt::DescendingOrder);
        break;
    case 2:  // 营收
        model->sort(model->fieldIndex("amount"), Qt::DescendingOrder);
        break;
    default:
        break;
    }
}

void TabContent::on_filterChanged(int index)
{
    QSqlTableModel *model = qobject_cast<QSqlTableModel*>(tableView->model());
    QString in = lineEditFilter->text();
    QString filter;
    switch (index) {
    case 0:  // 默认，无过滤
        model->setFilter("1=1");
        model->select();
        qDebug() << model->filter();
        break;
    case 1:  // 商品名称
        filter = QString("goodsShortName LIKE '%%1%'").arg(in);
        model->setFilter(filter);
        qDebug() << model->filter();
        break;
    case 2:  // 日期
        filter = QString("createTime LIKE '%%1%'").arg(in);
        model->setFilter(filter);
        qDebug() << model->filter();
        break;
    default:
        break;
    }
}

void TabContent::export2excel()
{
    //    const QString &file = "sales";
    //    const QString &sheetTitle = "test";
    //    QTableView *tableview = new QTableView;
    //    QSqlTableModel *tableModel = new QSqlTableModel;
    //    tableModel->setTable("sales");
    //    tableModel->select();
    //    tableview->setModel(tableModel);
    //    ExportExcelObject e(file, sheetTitle, tableview);
    //    e.addField(2, "name", "text");
    //    e.addField(3, "color", "text");
    //    e.export2Excel();
    Excel excel;
    excel.addSheet(excelSheetName);
    QSqlTableModel *model = qobject_cast<QSqlTableModel*>(tableView->model());
    qDebug() << "rows:" << model->rowCount() << ", cols:" << model->columnCount();
    QVariantList header;
    for (int i = 0; i < model->columnCount(); ++i) {
        header.append(model->headerData(i, Qt::Horizontal));
    }
    excel.appendRow(header);
    for (int i = 0; i < model->rowCount(); ++i) {
        QVariantList oneRow;
        for (int j = 0; j < model->columnCount(); ++j) {
            oneRow.append(model->record(i).value(j));
        }
        excel.appendRow(oneRow);
    }

    excel.save();
}

// opencv 拉流 rtsp  rtmp  播放  或记录到文件
// 客户端生成视频流  推到流媒体服务器 带音频
